<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回相对于 GMT 的时差</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="datetimezone.getname.html">« DateTimeZone::getName</a></li>
      <li style="float: right;"><a href="datetimezone.gettransitions.html">DateTimeZone::getTransitions »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.datetimezone.html">DateTimeZone</a></li>
    <li>返回相对于 GMT 的时差</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="datetimezone.getoffset" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTimeZone::getOffset</h1>
  <h1 class="refname">timezone_offset_get</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTimeZone::getOffset</span> -- <span class="refname">timezone_offset_get</span> &mdash; <span class="dc-title">返回相对于 GMT 的时差</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-datetimezone.getoffset-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateTimeZone::getOffset</strong></span>(<span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><a href="function.timezone-offset-get.html" class="methodname">timezone_offset_get</a></span>(<span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$object</code></span>, <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   该函数返回参数 <code class="parameter">datetime</code> 中指定的日期/时间相对于 GMT
   的时差。GMT 时差是通过 DateTimeZone 对象的时区信息计算出来的。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetimezone.getoffset-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>

<code class="parameter">object</code></dt>
<dd>
<p class="para">仅过程化风格：由 <span class="function"><a href="function.timezone-open.html" class="function">timezone_open()</a></span> 返回的
<span class="classname"><a href="class.datetimezone.html" class="classname">DateTimeZone</a></span> 对象。</p></dd>

    
     <dt>
<code class="parameter">datetime</code></dt>

     <dd>

      <p class="para">
       用来计算时差的 DateTime，其包含日期/时间。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetimezone.getoffset-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回精确到秒的时差， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-datetimezone.getoffset-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        在此之前的版本，失败时返回 <strong><code>false</code></strong>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetimezone.getoffset-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2044">
    <p><strong>示例 #1 <span class="function"><strong>DateTimeZone::getOffset()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;创建两个时区对象，分别是台北（台湾）和东京（日本）<br /></span><span style="color: #0000BB">$dateTimeZoneTaipei&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"Asia/Taipei"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dateTimeZoneJapan&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"Asia/Tokyo"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;创建两个包含相同&nbsp;Unix&nbsp;时间戳的&nbsp;DateTime&nbsp;对象。区别是对应的时区不同。<br /></span><span style="color: #0000BB">$dateTimeTaipei&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"now"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dateTimeZoneTaipei</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dateTimeJapan&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"now"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dateTimeZoneJapan</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;计算包含日期/时间的&nbsp;$dateTimeTaipei&nbsp;对象与时区规则定义为东京的&nbsp;$dateTimeZoneJapan&nbsp;对象的&nbsp;GMT&nbsp;时差<br /></span><span style="color: #0000BB">$timeOffset&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dateTimeZoneJapan</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getOffset</span><span style="color: #007700">(</span><span style="color: #0000BB">$dateTimeTaipei</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;应该展示&nbsp;int(32400)（Sat&nbsp;Sep&nbsp;8&nbsp;01:00:00&nbsp;1951&nbsp;JST&nbsp;之后的日期）。<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$timeOffset</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


</div></div></div></body></html>